package com.gitbl.webchat.web.control;

import com.alibaba.fastjson.JSONObject;
import com.gitbl.webchat.message.RoomMessage;
import com.gitbl.webchat.message.User;
import com.gitbl.webchat.web.webmessage.*;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPipeline;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController {




    @RequestMapping(value = "/info",method = RequestMethod.POST)
    public Object info(HttpServletRequest request) {
//        String token = request.getParameter("token").toLowerCase();
//        WebUser u = getUserByToken(token);
        WebUser u = getUser(request);
        WebError e=null;
        if(u==null)
        {
            e= WebError.NOTLOGIN;
        }
        return new Message(u,e);
    }
    @RequestMapping(value = "/exist",method = RequestMethod.POST)
    public Object validate(HttpServletRequest request) {
        String name = request.getParameter("name").toLowerCase();
        return new Message(userService.exist(name),null);
    }
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Object login(HttpServletRequest request) {
        String usernamel = request.getParameter("username").toLowerCase();

        User u = userService.login(usernamel,request.getParameter("pwd"));
        if (u==null)
            return new Message(null,WebError.LOGINFAULT);

        WebUser mu = Convert.parse(u);
        mu.setToken(UUID.randomUUID().toString());
        ShardedJedis rd = newShardedJedis();
        try {
            String oldtoken = rd.get(KEY_USER_PRE + u.getUserName());
            ShardedJedisPipeline pl = rd.pipelined();
            //删除旧token
            if (oldtoken != null) {
                pl.del(KEY_TOKEN_PRE + oldtoken);
            }

            pl.set(KEY_USER_PRE + mu.getUsername(), mu.getToken());
            pl.expire(KEY_USER_PRE + mu.getUsername(),TOKEN_TIME_OUT);

            pl.set(KEY_TOKEN_PRE + mu.getToken(), JSONObject.toJSONString(mu));
            pl.expire(KEY_TOKEN_PRE + mu.getToken(),TOKEN_TIME_OUT);

            pl.sync();

        }finally {
            rd.close();
        }
        return new Message(mu,null);
    }

    @RequestMapping(value = "/signup",method = RequestMethod.POST)
    public Object signup(HttpServletRequest request) {
        String  username = request.getParameter("username");
        String  nickname =request.getParameter("nickname");
        String  password =request.getParameter("password");
        if (username==null||nickname==null||password==null){
            return new Message(null,WebError.VALIDATEINPUT);
        }

        User u = new User();
        u.setNickName(username);
        u.setUserName(nickname);
        u.setPassword(password);

        return new Message(userService.signup(u),null);
    }
}
